package com.eax.ModelLayer;
import java.util.ArrayList;

public class AddressBookModel {
	private ArrayList<PersonModel> addressBook;

	public AddressBookModel(){
		addressBook = new ArrayList<PersonModel>();
	}

	public void addPerson(int id, String name, String address, int postalcode, String city, int telephone){
		PersonModel newPerson = new PersonModel(id, name, address, postalcode, city, telephone);
		addressBook.add(newPerson);
	}

	public void removePerson(PersonModel newPerson){
		addressBook.remove(newPerson);
	}

	public int getSize(){
		return addressBook.size();
	}

	public void printAllPeople(){
		for(PersonModel p : addressBook){
			p.printPerson();
			System.out.println();
		}
	}

	public PersonModel getPersonById(int id){
		PersonModel p = null;

		for(PersonModel x : this.addressBook){
			if(x.getId() == id){
				p = x;
			}
		}

		return p;
	}

	public int getHighestId(){
		int r = 0;

		for(PersonModel m : this.addressBook){
			if(m.getId() > r){
				r = m.getId();
			}
		}

		return r;
	}

	public ArrayList<PersonModel> getPeopleList(){
		return addressBook;
	}

	public PersonModel findPerson(String input){
		PersonModel m = null;

		for(PersonModel p : addressBook){
			Boolean correct = false;

			if(input.equals(Integer.toString(p.getId()))){
				correct = true;
			} else if(input.equals(p.getName())){
				correct = true;
			} else if(input.equals(p.getAddress())){
				correct = true;
			} else if(input.equals(Integer.toString(p.getTelephone()))){
				correct = true;
			}

			if(correct){
				m = p;
			}
		}

		return m;
	}
}